package unify.data;

import java.awt.Image;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collection;
import java.util.logging.Logger;

import javax.swing.ImageIcon;

import unify.ShowLibrary;
import unify.fileio.Database;
import unify.gui.SysTrayIcon;

import com.j256.ormlite.field.DatabaseField;
import com.j256.ormlite.field.ForeignCollectionField;
import com.j256.ormlite.table.DatabaseTable;


@DatabaseTable(tableName = "settings")
public class Settings {
	@SuppressWarnings("unused")
	@DatabaseField(generatedId = true)
	private int databaseId;
	@ForeignCollectionField(eager = true)
	private Collection<Search> searches = new ArrayList<Search>();
	@ForeignCollectionField(eager = true)
	private Collection<Feed> feeds = new ArrayList<Feed>();
	@DatabaseField
	private String today;
	@DatabaseField
	private String tomorrow;
	@DatabaseField
	private boolean findNewShows;
	@DatabaseField
	private boolean findMiniSeries;
	@DatabaseField
	private int delay;
	private final static Logger LOGGER = Logger.getLogger(ShowLibrary.class.getName());
	private static Settings _instance;

	/**
	 * 
	 */
	private Settings() {
		super();
	}

	public static synchronized Settings getInstance() {
		if (_instance == null) {
			_instance = Database.getSettings();
			if(_instance == null) {
				_instance = new Settings();
				_instance.setDefaults();
			}
		}
		return _instance;
	}

	private void setDefaults() {
		LOGGER.info("First run detected, loading default settings.");
		Search search = new Search("Amazon", "http://www.amazon.com/s/ref=nb_sb_noss_1?url=search-alias%3Dinstant-video&field-keywords=#showtitle#");
		Feed myFeed = new Feed("Hulu", "http://www.hulu.com/feed/recent/episode?cc_only=1", 10);
		searches.add(search);
		feeds.add(myFeed);
		this.today = "TVRage::http://www.tvrage.com/myrss.php";
		this.tomorrow = "TVRage::http://www.tvrage.com/myrss.php?date=tomorrow";
		this.findNewShows = true;
		this.findMiniSeries = true;
		this.delay = 60;
	}

	//Search urls
	public void addSearch(String label, String link) {
		this.searches.add(new Search(label, link));
	}
	
	public Collection<Search> getSearches() {
		return this.searches;
	}
	
	public void removeSearch(int index) {
		ArrayList<Search> searches = new ArrayList<Search>();
		searches.addAll(this.searches);
		Search search = searches.get(index);
		this.searches.remove(search);
	}
	
	//Feeds
	public int getFeedPriority(String feedName) {
		for(Feed feed : feeds) {
			if(feedName.equalsIgnoreCase(feed.getLabel())) {
				return feed.getPriority();
			}
		}
		return -1;
	}
	
	public Collection<Feed> getFeeds() {
		return feeds;
	}
	
	public void addFeed(Feed feed) {
		this.feeds.add(feed);
	}
	
	public void removeFeed(int index) {
		ArrayList<Feed> feeds = new ArrayList<Feed>();
		feeds.addAll(this.feeds);
		Feed feed = feeds.get(index);
		this.feeds.remove(feed);
	}
	
	//Other getters and setters
	public int getDelay() {
		return delay;
	}

	public void setDelay(int delay) {
		this.delay = delay;	
	}

	public boolean isFindNewShows() {
		return findNewShows;
	}
	
	public void setFindNewShows(boolean findNewShows) {
		this.findNewShows = findNewShows;
	}

	public boolean isFindMiniSeries() {
		return findMiniSeries;
	}

	public void setFindMiniSeries(boolean findMiniSeries) {
		this.findMiniSeries = findMiniSeries;
	}

	public Image getIconImage() {
		URL url = SysTrayIcon.class.getResource("/icon.png");
		return (new ImageIcon(url, "Unify")).getImage();
	}

	public Feed getToday() {
		return new Feed(today.split("::")[0], today.split("::")[1], 0);
	}

	public void setToday(String link) {
		this.today = link;
	}

	public Feed getTomorrow() {
		return new Feed(tomorrow.split("::")[0], tomorrow.split("::")[1], 0);
	}

	public void setTomorrow(String link) {
		this.tomorrow = link;
	}
}
